<template>
  <e-table
    @getList="handleGetList"
    :tableData="tableData"
    :columns="columns"
    :loading="loading"
    :pageObj="null"
  >
    <el-table-column slot="action" label="操作" fixed="right" align="center">
      <template slot-scope="scope">
        <el-button type="text" @click.native="handleEdit(true, scope.row)">修改</el-button>
        <el-button type="text" @click.native="handleDelete(scope.row)" class="red">删除</el-button>
        <el-button type="text" @click.native="handleEdit(false, scope.row)">复制</el-button>
        <el-button type="text" @click.native="handleDesign(scope.row)">设计</el-button>
      </template>
    </el-table-column>
  </e-table>
</template>

<script>
import { getUreportAll, ureportDelete } from '@/api/system'
import { openNotification, formatTime, serviceParam } from '@/utils/util'

export default {
  data() {
    return {
      loading: false,
      // 表头
      columns: [
        {
          label: '类型',
          prop: 'ureportType',
          formatter(row, column, cellValue, index) {
            return cellValue === 1 ? '工作流' : cellValue === 2 ? '业务系统' : '其他'
          }
        },
        {
          label: '名称',
          prop: 'ureportName'
        },
        {
          label: '下载文件名',
          prop: 'downloadName'
        },
        {
          label: '更新时间',
          prop: 'updateDate',
          formatter(row, column, cellValue, index) {
            return formatTime(cellValue)
          }
        },
        { slot: 'action' }
      ],
      tableData: []
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    handleDesign(row) {
      let obj = serviceParam('serverUrl')
      let uri = process.env.NODE_ENV === 'production' ? obj.paraValue : 'http://192.168.14.2:8888/'
      const name = row.dowloadName ? row.dowloadName : ''
      window.open(`${uri}ureport/designer?_u=${row.ureportType}:${row.ureportName}&_n=${name}`)
    },
    handleEdit(bool, record) {
      this.$nextTick(() => {
        this.$emit('edit', bool, record)
      })
    },
    // 删除
    handleDelete(row) {
      console.log('row', row)
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.ureportName}]记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await ureportDelete(row.ureportId, {})
          openNotification('success', '删除成功')
          _this.handleGetList()
        }
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      const res = await getUreportAll(param)
      if (res.data && res.data.length > 0) {
        const { state, data } = res
        this.tableData = data
      } else {
        this.tableData = []
      }
      this.loading = false
    }
  }
}
</script>
